package com.example.yls.demoa;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private EditText edtNo;
    private EditText edtName;
    private EditText edtAge;
    private Button btnInsertStu;
    private Button btnQuery;
    private Button btnDelete;
    private Button btnUpdate;
    private TextView txtResult;

    private Button insertSco;
    private TextView scoResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        edtNo=findViewById(R.id.edt_No);
        edtName=findViewById(R.id.edt_Name);
        edtAge=findViewById(R.id.edt_Age);
        btnInsertStu=findViewById(R.id.btn_insert);
        btnInsertStu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Uri uri=Uri.parse("content://com.gdcp.student/student");
                ContentValues contentValues = new ContentValues();
                String no = edtNo.getText().toString();
                String name = edtName.getText().toString();
                int age = Integer.valueOf(edtAge.getText().toString());
                contentValues.put("stuno",name);
                contentValues.put("name",no);
                contentValues.put("age",age);
               /* contentValues.put("stuno","171001");
                contentValues.put("name","lizi");
                contentValues.put("age",20);*/
                getContentResolver().insert(uri,contentValues);
            }
        });
        btnQuery=findViewById(R.id.btn_query);
        txtResult=findViewById(R.id.txt_result);
        btnQuery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Uri uri=Uri.parse("content://com.gdcp.student/student");
                Cursor cursor = getContentResolver().query(uri,null,null,null,null);
                if(cursor!=null && cursor.getCount()>0){
                    StringBuffer buffer = new StringBuffer();
                    for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()){
                        String stuno = cursor.getString(0);
                        String name=cursor.getString(1);
                        int age = cursor.getInt(2);
                        buffer.append(stuno).append(" ").append(name).append("  ").append(age).append("\n");
                        txtResult.setText(buffer);
                    }
                }
            }
        });

        btnDelete= findViewById(R.id.btn_delete);
        btnUpdate= findViewById(R.id.btn_update);

        btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Uri uri = Uri.parse("content://com.gdcp.student/student");
                String where = "name='lizi'";
                int rows = getContentResolver().delete(uri,where,null);
                Toast.makeText(MainActivity.this,"删除了"+ rows +"行",Toast.LENGTH_SHORT).show();
            }
        });
            //插入成绩表和查询成绩
        insertSco=findViewById(R.id.btn_insertsco);
        insertSco.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Uri uri=Uri.parse("content://com.gdcp.student/score");
                ContentValues contentValues = new ContentValues();
                contentValues.put("stuno","171001");
                contentValues.put("java",90);
                contentValues.put("android",90);
                getContentResolver().insert(uri,contentValues);
            }
        });
        scoResult=findViewById(R.id.score_result);
        scoResult.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Uri uri=Uri.parse("content://com.gdcp.student/score");
                Cursor cursor = getContentResolver().query(uri,null,null,null,null);
                if(cursor!=null && cursor.getCount()>0) {
                    StringBuffer buffer = new StringBuffer();
                    for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
                        String stuno = cursor.getString(0);
                        int sjava = cursor.getInt(1);
                        int android = cursor.getInt(2);
                        buffer.append(stuno).append(" ").append(sjava).append("  ").append(android).append("\n");
                        txtResult.setText(buffer);
                    }
                }
            }
        });
    }
}